package zpf.feign;

import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import zpf.entity.User;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: HystrixClientFallbackFactory
 * @Description: TODO
 * @Autor: ZPF
 * @Date: 2021/1/15 10:26
 **/
@Slf4j
@Component
public class HystrixClientFallbackFactory implements FallbackFactory<UserFeignHystrixFactoryClient> {
    @Override
    public UserFeignHystrixFactoryClient create(Throwable e) {
        log.info("fallback; reason was: {}", e.getMessage());
        System.out.println("== UserFeignHystrixFactoryClient.create " + Thread.currentThread().getThreadGroup() + " - " + Thread.currentThread().getId() + " - " + Thread.currentThread().getName());

        return new UserFeignWithFallBackFactoryClient() {
            @Override
            public User findById(Long id) {
                System.out.println("== findById FallBackFactory " + Thread.currentThread().getThreadGroup() + " - " + Thread.currentThread().getId() + " - " + Thread.currentThread().getName());

                User tmpUser = new User();
                tmpUser.setId(-1L);
                return tmpUser;
            }

            @Override
            public List<User> findAll() {
                return new ArrayList<>();
            }
        };
    }
}
